package com.hqd.jjvm.jnative;

import com.hqd.jjvm.rtda.heap.JMethod;

import java.lang.reflect.InvocationTargetException;

public class JNativeRegistryFactory {

    public static JNativeFunction getInstance(JMethod jMethod) {
        String classPath = jMethod.getJClass().getClassName();
        String simpleName = classPath.substring(classPath.lastIndexOf("/") + 1);
        classPath = classPath.substring(0, classPath.lastIndexOf("/"));
        String packagePath = classPath.substring(classPath.lastIndexOf("/") + 1);
        JNativeFunction function = null;
        try {
            String packageName = "com.hqd.jjvm.jnative." + packagePath + ".";
            Class<JNativeRegistry> instanceClazz = (Class<JNativeRegistry>) Class.forName(packageName + "J" + simpleName + "NativeRegistry");
            instanceClazz.getConstructor().newInstance();
            function = JNativeRegistry.getJNativeFunction(jMethod);
        } catch (ClassNotFoundException | NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return function;
    }
}
